<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/19
 * Time: 11:04
 */

class Base
{
    //监听ip
    protected $host;
    //监听端口
    protected $port;
    //服务对象
    protected $httpServer;

    public function __construct($port = 9051, $host = '0.0.0.0')
    {
        $this->host = $host;
        $this->port = $port;
    }

    public function http()
    {
        // 实例化swoole http对象
        $this->httpServer = new Swoole\Http\Server($this->host, $this->port);
        //设置访问静态资源文件
        $this->httpServer->set([
            'document_root' => 'G:\haibei\websocket\swoole\demo\static',
            'enable_static_handler' => true,
        ]);
        //监听请求事件
        $this->httpServer->on('request', [$this, 'http_request']);
        // 开启服务
        $this->httpServer->start();
    }

    //监听http请求事件
    public static function http_request(Swoole_Http_Request $request, Swoole_Http_Response $response)
    {
        echo '请求成功' . $request->fd;
        //返回客户端消息
        $response->end("<head><meta charset='UTF-8'></head><h1>请求成功. #" . rand(1000, 9999) . "</h1>");
    }

    // 启动webSocket服务
    public function webSocket()
    {
        $server = new Swoole\WebSocket\Server($this->host, $this->port);
// 监听客户端链接并打开(有客服端有链接就会触发)
        $server->on('open', [$this, 'webSocket_onOpen']);
        $server->on('message', [$this, 'webSocket_message']);
        $server->on('close', [$this, 'webSocket_close']);
        $server->start();
    }

    /**
     * @param \Swoole\WebSocket\Server $server
     * @param \Swoole\Http\Request $request
     * 创建链接
     */
    public static function webSocket_onOpen(Swoole\WebSocket\Server $server, Swoole\Http\Request $request)
    {
        print_r($request->header);
    }


    //监听客服端发送的消息  $frame 接受客户端发送的消息
    public static function webSocket_message(Swoole\WebSocket\Server $server, swoole_websocket_frame $frame)
    {
        $server->push($frame->fd, 1111);
    }


    //客服端关闭链接
    public static function webSocket_close(Swoole\WebSocket\Server $server, $fd)
    {
    }

    public function tcp()
    {
        $server = new swoole_server("0.0.0.0", 9510);
        $server->set([
            'worker_num' => 3,//开启进程数量
            'max_request' => 1000 //参数表示worker进程在处理完n次请求后结束运行。manager会重新创建一个worker进程。此选项用来防止worker进程内存溢出
        ]);

//监听连接进入事件
        $server->on('connect', [$this, 'tcp_connect']);

//监听数据接收事件
        $server->on('receive', [$this, 'tcp_receive']);

//监听连接关闭事件
        $server->on('close', [$this, 'tcp_close']);

//启动服务器
        $server->start();
    }

    //监听连接进入事件
    public static function tcp_connect(swoole_server $server, $fd, $processId)
    {
        echo '链接fd---》' . $fd . "\n";
        echo "链接线程Id: {$processId}.\n";
    }

//监听数据接收事件
    public static function tcp_receive(swoole_server $server, $fd, $processId, $data)
    {
        $server->send($fd, "Server:线程id：{$processId} " . $data);
    }

//监听连接关闭事件
    public static function tcp_close(swoole_server $server, $fd)
    {
        echo "Client: Close.fd--->{$fd}\n";
    }

}